package com.bwie.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdInfoMapper;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdInfo;
import com.bwie.pojo.TbType;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.ProdInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.Date;
import java.util.List;

/**
* @author 叶雨洁
* @description 针对表【tb_prod(商品表)】的数据库操作Service实现
* @createDate 2025-07-19 10:27:06
*/
@Service
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper prodMapper;

    @Autowired
    TbTypeMapper typeMapper;

    @Autowired
    TbProdInfoMapper prodInfoMapper;

    @Override
    public R pageList(PageInfoVo pageInfoVo) {

        Page<TbProd> tbProdPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbProd> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
           queryWrapper.lambda().like(TbProd::getProdName,pageInfoVo.getKeyWord());
        }
        Page<TbProd> tbProdPage1 = prodMapper.selectPage(tbProdPage, queryWrapper);
        return R.OK(tbProdPage1);
    }

    @Override
    public R deleteBatch(List<TbProd> ids) {


        for (TbProd tbProd : ids) {
            prodMapper.deleteById(tbProd);
        }
        return R.OK();
    }

    //分类表单
    @Override
    public R prodTreeList(PageInfoVo pageInfoVo) {

        QueryWrapper<TbType> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            queryWrapper.lambda().like(TbType::getTypeName,pageInfoVo.getKeyWord());
        }

        if(pageInfoVo.getOrderType()!=null){
            queryWrapper.lambda().eq(TbType::getTypeStatus,pageInfoVo.getOrderType());
        }

        List<TbType> tbProds = typeMapper.selectList(queryWrapper);
         if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord()) || pageInfoVo.getOrderStatus()!=null){
             return R.OK(tbProds);
         }

        List<Tree<String>> build = TreeUtil.build(tbProds, "0", (menu, tree) -> {
            tree.setId(menu.getTypeId() + "");
            tree.setParentId(menu.getTypePid() + "");
            tree.setName(menu.getTypeName());
            tree.putExtra("createBy", menu.getCreateBy());
            tree.putExtra("typeStatus", menu.getTypeStatus());
            tree.putExtra("typeImage", menu.getTypeImage());
            tree.putExtra("updateTime", menu.getUpdateTime());
            tree.putExtra("updateBy", menu.getUpdateBy());
            tree.putExtra("createTime", menu.getCreateTime());
            tree.putExtra("typeTop", menu.getTypeTop());
        });

        return R.OK(build);
    }

    @Override
    public R classfitySave(TbType tbType) {

        if(tbType.getTypePid()==null){
            tbType.setTypePid(0);
        }

        tbType.setCreateTime(new Date());
        tbType.setCreateBy("admin");
        tbType.setTypeStatus(0);
        typeMapper.insert(tbType);
        return R.OK();
    }

    @Override
    public R saveProd(TbProd tbProd) {


        tbProd.setCreateTime(new Date());
        tbProd.setCreateBy("admin");


        TbProd tbProd1 = prodMapper.selectOne(
                new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName, tbProd.getProdName()).eq(TbProd::getProdSpu,tbProd.getProdSpu())
        );
        if(tbProd1!=null){
            return R.ERROR(500,"商品已存在");
        }
        tbProd.setCreateBy("admin");
        tbProd.setCreateTime(new Date());
        prodMapper.insert(tbProd);
        return R.OK();
    }

    @Override
    public R getProdInfo(Integer prodId) {

        TbProd tbProd = prodMapper.selectById(prodId);
        if(tbProd==null){
            return R.ERROR(500,"商品不存在");
        }
        //存储到redis缓存中


        return R.OK(tbProd);
    }

    @Override
    public R saveProdInfo(TbProdInfo tbProdInfo) {

        TbProd byId = getById(tbProdInfo.getProdId());

        if(byId==null){
            return R.ERROR(500,"商品不存在");
        }

        LambdaQueryWrapper<TbProdInfo> wrapper = new QueryWrapper<TbProdInfo>().lambda().eq(TbProdInfo::getProdId, tbProdInfo.getProdId()).eq(TbProdInfo::getProdName, tbProdInfo.getProdName());
        TbProdInfo tbProdInfo1 = prodInfoMapper.selectOne(wrapper);
        if(tbProdInfo1!=null){
            return R.ERROR(500,"商品规格已存在");
        }

        tbProdInfo.setCreateTime(new Date());
        tbProdInfo.setCreateBy("admin");
        prodInfoMapper.insert(tbProdInfo);
//        prodMapper.insert(tbProdInfo);
        return R.OK();
    }
}




